<?php

namespace App\Http\Controllers\Manage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use App\Model\Industry;

class IndustryController extends Controller
{

    /**
     * 案例行业列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function industryList() {
        $orderBy = Input::get('orderBy');
        $page = Input::get('page');
        $count = Input::get('count');
        $obj = new Industry();
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $industry =$obj->get();
        if(empty($page)){
            $count = $obj->count();
        }

        return view('manage.case.industryList',[
            'industry' => $industry,
            'count' => $count,
            'active1'=>8,
            'active2'=>1
        ]);
    }

    /**
     * 行业启禁用
     * @return array
     */
    public function industryStatus() {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 1) {
            $del = Industry::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $del = Industry::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }

    /**
     * 载入添加页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function industryAdd()
    {
        return view('manage.case.industryAdd',['active1'=>8,'active2'=>2]);
    }

    /**
     * 添加行业
     * @return array
     */
    public function industry(){
        $rank = input::get('rank');
        $name = input::get('name');
//        return 123123;
        if(empty($name)) {
            return [
                'code' => 103,
                'msg' => '缺少参数!'
            ];
        }
        $list = Industry::where('name',$name)->first();
        if($list) {
            return [
                'code' => 102,
                'msg' => '该行业已存在!'
            ];
        }
        $map = Industry::insert([
            'rank' => $rank,
            'name' => $name,
        ]);
        if($map) {
            return [
                'code' => 100,
                'msg' => '添加成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' => '添加失败!'
            ];
        }
    }

    /**
     * 获取行业ID
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function industryEdit($id) {
        $industry = Industry::where('id',$id)->first();
        return view('manage.case.industryUpdate',['industry' => $industry,'active1'=>8,'active2'=>1]);
    }

    /**
     * 更新行业资料
     * @return array
     */
    public function industryUpdate() {
        $id = input::get('id');
        $data['rank'] = input::get('rank');
        $data['name'] = input::get('name');
        if(empty($data['name'])) {
            return [
                'code' => 103,
                'msg' => '缺少参数!'
            ];
        }
//        $list = Industry::where('name',$data['name'])->first();
//        if($list) {
//            return [
//                'code' => 102,
//                'msg' => '该名称已存在!'
//            ];
//        }
        $map = Industry::where('id',$id)->update($data);
        if($map) {
            return [
                'code' => 100,
                'msg' => '更新成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' => '更新失败!'
            ];
        }
    }

    /**
     * 删除行业
     * @return array
     */
    public function industryDelete()
    {
        $id = Input::get('id');
        $res = Industry::where('id',$id)->delete();
        if($res){
            return [
                'code'=>100,
                'msg'=>'删除成功！'
            ];
        }else{
            return [
                'code'=>101,
                'msg'=>'删除失败！'
            ];
        }
    }
}
